package com.fr.work.job;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

public class ESWrapper {
	public static boolean isMonitor = true;
	public static final int MAX_WAITING_TIME = 5;

	private static int AVAILABLEPROCESSORS = Runtime.getRuntime()
			.availableProcessors();

	public static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
			AVAILABLEPROCESSORS * 50, AVAILABLEPROCESSORS * 80, 0L,
			TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(
					AVAILABLEPROCESSORS * 2000), new ThreadFactoryBuilder()
					.setNameFormat("ESWrapper-pool-Thread-%d").build());
	public static Set<String> noQuerySet = null;
	static {
		if (isMonitor) {
			noQuerySet = new HashSet<>();
		}
	}

	public static void mothed(String key, String value) {
		try {

		} catch (Exception e) {

		} finally {
			if (isMonitor) {
				threadPoolExecutor.execute(new MonitorRunnable(key, value));
			}
		}
	}

	static private void addNoQuerySet(String key) {
		synchronized (noQuerySet) {
			noQuerySet.add(key);
		}
	}

	static class MonitorRunnable implements Runnable {
		private String key;
		private String value;

		public MonitorRunnable(String key, String value) {
			this.key = key;
			this.value = value;
		}

		@Override
		public void run() {
			if (value.contains("2")) {
				addNoQuerySet(key);
			}
		}

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}
	}

}
